package weaver.interfaces.cese.duss.action.srm;

import com.engine.cese.duss.srm.util.CalUtil;
import com.weaver.general.BaseBean;
import org.docx4j.wml.R;
import weaver.conn.RecordSet;
import weaver.docs.webservices.DocInfo;
import weaver.docs.webservices.DocServiceImpl;
import weaver.formmode.customjavacode.modeexpand.srm.util.SrmUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-10-23
 * @Description: 公告发布流程
 * @ModifyLog:
 **/
/*
 *@Description: 答疑澄清流程清除明细数据
 *@Param:
 *@return:
 *@Author: Dusongsong
 *@Date: 2020/12/10
 */
public class WeaDycqDelDtlAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        writeLog( "----WeaDycqDelDtlAction-------" + requestid );
        try {
            rs.executeQuery( "select id,cglxd,lc,zcgcbz,sfgxxq from " + tablename + " where requestid=?", requestid );
            rs.next();
            String cglxd = Util.null2String( rs.getString( "cglxd" ) );//采购立项单
            String lc = Util.null2String( rs.getString( "lc" ) );//轮次
            String zcgcbz = Util.null2String( rs.getString( "zcgcbz" ) );//招采过程步骤
            String sfgxxq = Util.null2String( rs.getString( "sfgxxq" ) );//是否需求更新
            String zbcglx = baseBean.getPropValue( "SrmWfTab", "zbcglx" );
            String fqpb = baseBean.getPropValue( "SrmWfTab", "fqpb" );
            String sql1 = "select cglx from " + zbcglx + " where requestid=?";
            rs.executeQuery( sql1, cglxd );
            rs.next();
            String cglx = Util.null2String( rs.getString( "cglx" ) );
            if (("6".equals( zcgcbz ) && "0".equals( sfgxxq )) || ("8".equals( zcgcbz ) && "0".equals( sfgxxq ))) {
                insertFqpbDtl( cglx, fqpb, zbcglx, cglxd, lc );
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            writeLog( "异常参考:" + e );
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }

    //插入发起评标明细数据
    public static void insertFqpbDtl(String cglx, String fqpb, String zbcglx, String cglxd, String lc) {
        RecordSet rs = new RecordSet();
        rs.writeLog( "------insertFqpbDtl-------" );
        rs.writeLog( "参数=" + cglx + "," + fqpb + "," + zbcglx + "," + cglxd + "," + lc );
        String cgrwtable = SrmUtil.getCgrwTableType( cglx );
        rs.executeQuery( "select id from " + fqpb + " where cglxd=? and lc=?", cglxd, lc );
        rs.next();
        String mainid = Util.null2String( rs.getString( "id" ) );
        rs.writeLog( "mainid=" + mainid );
        String zbcglxDtl = zbcglx + "_dt" + CalUtil.fAdd(cglx,"2");
        String sql = "select wm_concat(t2.rwdid) from " + zbcglxDtl + " t2 " +
                "left join " + zbcglx + " t on t2.mainid=t.id where t.requestid=?";
        rs.executeQuery( sql, cglxd );
        rs.writeLog( sql, cglxd );
        rs.next();
        String rwdid = Util.null2String( rs.getString( 1 ) );
        switch (cglx) {
            case "0":
                String sql11 = "delete " + fqpb + "_dt4 where mainid=?";
                rs.executeUpdate( sql11, mainid );
                String sql1 = "select t.id,t.xmmc,t.xqbh,t.bz,t1.fb,t1.zfb,t1.wlbm,t1.fbbh,t1.zfbbh,t1.zfbid,t1.wlid, " +
                        "t1.wlmc,t1.cms,t1.xlh,t1.xlbz,t1.dw,t1.sl,t1.fzdw,t1.fzsl " +
                        "from " + cgrwtable + " t left join " + cgrwtable + "_dt1 t1 on t.id=t1.mainid where t.id in(" + rwdid + ")";
                rs.executeQuery( sql1 );
                rs.writeLog( sql1 );
                while (rs.next()) {
                    String id = Util.null2String( rs.getString( "id" ) );
                    String xmmc = Util.null2String( rs.getString( "xmmc" ) );
                    String xqbh = Util.null2String( rs.getString( "xqbh" ) );
                    String bz = Util.null2String( rs.getString( "bz" ) );
                    String fb = Util.null2String( rs.getString( "fb" ) );
                    String zfb = Util.null2String( rs.getString( "zfb" ) );
                    String wlbm = Util.null2String( rs.getString( "wlbm" ) );
                    String fbbh = Util.null2String( rs.getString( "fbbh" ) );
                    String zfbbh = Util.null2String( rs.getString( "zfbbh" ) );
                    String zfbid = Util.null2String( rs.getString( "zfbid" ) );
                    String wlid = Util.null2String( rs.getString( "wlid" ) );
                    String wlmc = Util.null2String( rs.getString( "wlmc" ) );
                    String cms = Util.null2String( rs.getString( "cms" ) );
                    String xlh = Util.null2String( rs.getString( "xlh" ) );
                    String xlbz = Util.null2String( rs.getString( "xlbz" ) );
                    String dw = Util.null2String( rs.getString( "dw" ) );
                    String sl = Util.null2String( rs.getString( "sl" ) );
                    String fzdw = Util.null2String( rs.getString( "fzdw" ) );
                    String fzsl = Util.null2String( rs.getString( "fzsl" ) );
                    String sql2 = "insert into " + fqpb + "_dt4 (xmmc,xqbh,rwdid,bz,fb,zfb,wlbm," +
                            "fbbh,zfbbh,zfbid,wlid,mainid,wlmc,cms,xlh,xlbz,dw,sl,fzdw,fzsl)" +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
                    Object[] params = new Object[]{xmmc, xqbh, id, bz, fb, zfb, wlbm, fbbh, zfbbh, zfbid, wlid, mainid, wlmc, cms, xlh, xlbz, dw, sl, fzdw, fzsl};
                    rs.executeUpdate( sql2, params );
                }
                break;
            case "1":
                String sql12 = "delete " + fqpb + "_dt5 where mainid=?";
                rs.executeUpdate( sql12, mainid );
                String sql2 = "select t.id,t.xmmc,t.xqbh,t1.fb,t1.zfb,t1.wlbm,t1.fbbh,t1.zfbbh,t1.zfbid,t1.wlid, " +
                        "t1.wlmc,t1.cms,t1.xlh,t1.xlbz,t1.dw,t1.sl,t1.fzdw,t1.fzsl " +
                        "from " + cgrwtable + " t left join " + cgrwtable + "_dt1 t1 on t.id=t1.mainid where t.id in(" + rwdid + ")";
                rs.executeQuery( sql2 );
                rs.writeLog( sql2 );
                while (rs.next()) {
                    String id = Util.null2String( rs.getString( "id" ) );
                    String xmmc = Util.null2String( rs.getString( "xmmc" ) );
                    String xqbh = Util.null2String( rs.getString( "xqbh" ) );
                    String fb = Util.null2String( rs.getString( "fb" ) );
                    String zfb = Util.null2String( rs.getString( "zfb" ) );
                    String wlbm = Util.null2String( rs.getString( "wlbm" ) );
                    String fbbh = Util.null2String( rs.getString( "fbbh" ) );
                    String zfbbh = Util.null2String( rs.getString( "zfbbh" ) );
                    String zfbid = Util.null2String( rs.getString( "zfbid" ) );
                    String wlid = Util.null2String( rs.getString( "wlid" ) );
                    String wlmc = Util.null2String( rs.getString( "wlmc" ) );
                    String cms = Util.null2String( rs.getString( "cms" ) );
                    String xlh = Util.null2String( rs.getString( "xlh" ) );
                    String xlbz = Util.null2String( rs.getString( "xlbz" ) );
                    String dw = Util.null2String( rs.getString( "dw" ) );
                    String sl = Util.null2String( rs.getString( "sl" ) );
                    String fzdw = Util.null2String( rs.getString( "fzdw" ) );
                    String fzsl = Util.null2String( rs.getString( "fzsl" ) );
                    String sql3 = "insert into " + fqpb + "_dt5(xmmc,xqbh,rwdid,fb,zfb,wlbm," +
                            "fbbh,zfbbh,zfbid,wlid,mainid,wlmc,cms,xlh,xlbz,dw,sl,fzdw,fzsl)" +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";
                    Object[] params = new Object[]{xmmc, xqbh, id, fb, zfb, wlbm, fbbh, zfbbh, zfbid, wlid, mainid, wlmc, cms, xlh, xlbz, dw, sl, fzdw, fzsl};
                    rs.executeUpdate( sql3, params );
                }
                break;
            case "2":
                String sql13 = "delete " + fqpb + "_dt6 where mainid=?";
                rs.executeUpdate( sql13, mainid );
                String sql3 = "select t.id,t.xmmc,t.xqbh,t.bz,t1.jjdw,t1.jjsl,t1.mc,t1.ggxh,t1.dw,t1.sl,t1.qzsj,t1.jssj\n" +
                        "from " + cgrwtable + " t left join " + cgrwtable + "_dt1 t1 on t.id=t1.mainid  where t.id in(" + rwdid + ")";
                rs.executeQuery( sql3 );
                rs.writeLog( sql3 );
                while (rs.next()) {
                    String id = Util.null2String( rs.getString( "id" ) );
                    String xmmc = Util.null2String( rs.getString( "xmmc" ) );
                    String xqbh = Util.null2String( rs.getString( "xqbh" ) );
                    String bz = Util.null2String( rs.getString( "bz" ) );
                    String jjdw = Util.null2String( rs.getString( "jjdw" ) );
                    String jjsl = Util.null2String( rs.getString( "jjsl" ) );
                    String mc = Util.null2String( rs.getString( "mc" ) );
                    String ggxh = Util.null2String( rs.getString( "ggxh" ) );
                    String dw = Util.null2String( rs.getString( "dw" ) );
                    String sl = Util.null2String( rs.getString( "sl" ) );
                    String qzsj = Util.null2String( rs.getString( "qzsj" ) );
                    String jssj = Util.null2String( rs.getString( "jssj" ) );
                    String sql4 = "insert into " + fqpb + "_dt6(xqbh,rwdid,mainid,mc,ggxh,dw,sl,qzsj,jssj,xmmc,bz,jjdw,jjsl)" +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?,?) ";
                    Object[] params = new Object[]{xqbh, id, mainid, mc, ggxh, dw, sl, qzsj, jssj, xmmc, bz, jjdw, jjsl};
                    rs.executeUpdate( sql4, params );
                }
                break;
            case "3":
                String sql14 = "delete " + fqpb + "_dt7 where mainid=?";
                rs.executeUpdate( sql14, mainid );
                String sql4 = "select t.id,t.xmmc,t.xqbh,t1.hwmc,t1.ggxh,t1.dw,t1.sl,t1.qydd,t1.dddd,t1.qyrq,t1.dhrq,t1.ysfs\n" +
                        "from " + cgrwtable + " t left join " + cgrwtable + "_dt1 t1 on t.id=t1.mainid   where t.id in(" + rwdid + ")";
                rs.executeQuery( sql4 );
                rs.writeLog( sql4 );
                while (rs.next()) {
                    String id = Util.null2String( rs.getString( "id" ) );
                    String xmmc = Util.null2String( rs.getString( "xmmc" ) );
                    String xqbh = Util.null2String( rs.getString( "xqbh" ) );
                    String hwmc = Util.null2String( rs.getString( "hwmc" ) );
                    String ggxh = Util.null2String( rs.getString( "ggxh" ) );
                    String dw = Util.null2String( rs.getString( "dw" ) );
                    String sl = Util.null2String( rs.getString( "sl" ) );
                    String qydd = Util.null2String( rs.getString( "qydd" ) );
                    String dddd = Util.null2String( rs.getString( "dddd" ) );
                    String qyrq = Util.null2String( rs.getString( "qyrq" ) );
                    String dhrq = Util.null2String( rs.getString( "dhrq" ) );
                    String ysfs = Util.null2String( rs.getString( "ysfs" ) );
                    String sql5 = "insert into " + fqpb + "_dt7( xqbh,rwdid,xmmc,mainid,hwmc,ggxhckg,dw,sl,qydd,dddd,qyrq,dhrq,ysfs)" +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?,?) ";
                    Object[] params = new Object[]{xqbh, id, xmmc, mainid, hwmc, ggxh, dw, sl, qydd, dddd, qyrq, dhrq, ysfs};
                    rs.executeUpdate( sql5, params );
                }
                break;
            case "4":
                String sql15 = "delete " + fqpb + "_dt9 where mainid=?";
                rs.executeUpdate( sql15, mainid );
                String sql5 = "select t.id,t.xmmc,t.xqbh,t.bz,t1.fwnr,t1.fwyq,t1.dw,t1.sl,t1.zlyq,t1.ksrq,t1.jsrq\n" +
                        "from " + cgrwtable + " t left join " + cgrwtable + "_dt1 t1 on t.id=t1.mainid  where t.id in(" + rwdid + ")";
                rs.executeQuery( sql5 );
                rs.writeLog( sql5 );
                while (rs.next()) {
                    String id = Util.null2String( rs.getString( "id" ) );
                    String xmmc = Util.null2String( rs.getString( "xmmc" ) );
                    String xqbh = Util.null2String( rs.getString( "xqbh" ) );
                    String bz = Util.null2String( rs.getString( "bz" ) );
                    String fwnr = Util.null2String( rs.getString( "fwnr" ) );
                    String fwyq = Util.null2String( rs.getString( "fwyq" ) );
                    String dw = Util.null2String( rs.getString( "dw" ) );
                    String sl = Util.null2String( rs.getString( "sl" ) );
                    String zlyq = Util.null2String( rs.getString( "zlyq" ) );
                    String ksrq = Util.null2String( rs.getString( "ksrq" ) );
                    String jsrq = Util.null2String( rs.getString( "jsrq" ) );
                    String sql6 = "insert into " + fqpb + "_dt9(xmmc,dw,mainid,fwnr,fwyq,xqbh,sl,zlyq,ksrq,jsrq,rwdid,bz)" +
                            "values(?,?,?,?,?,?,?,?,?,?,?,?) ";
                    Object[] params = new Object[]{xmmc, dw, mainid, fwnr, fwyq, xqbh, sl, zlyq, ksrq, jsrq, id, bz};
                    rs.executeUpdate( sql6, params );
                }
                break;
        }
    }
}
